%% batch select files to process:

% stuff to potentially change depending on experiment:
datatype = 'gcamp+ios'; %'gcamp+ios' OR 'ios1ch'
stimtype = 'stationary';   %'optogenetic' OR 'stationary' OR 'moving' OR 'whisker'
numsessions = 3;
movtype = 'gcamp';  %'gcamp' (for gcamp+ios) OR 'ios' (for multispectral ios) OR 'ios1ch' (for just hbt)
% probably don't change stuff below here
refsession = input('input reference session: ');
cameraID = 'dalsa';
externalReferenceSession = false; %set this to true if the reference session (to which everything is registered) is NOT one of the sessions being averaged here

metanames = cell(1,numsessions);
metadirectories = cell(1,numsessions);
metanamesBG = cell(1,numsessions);
metadirectoriesBG = cell(1,numsessions);
movdirectories = cell(1,numsessions);
fn = cell(1,numsessions);
rootnames = cell(1,numsessions);
windowmasks = cell(1,numsessions);
imsizes = zeros(numsessions,2);
homedir = cd;

pixelbinning = zeros(numsessions,1);
skipmanualselect = true;        %use this if manual selections already done
savename = input('root name for saving: ','s');
savedirectory = uigetdir('\\research.files.med.harvard.edu\neurobio\GU LAB\WIDEFIELD\visualStim','select save directory');

if externalReferenceSession
    [referenceImageFN,referenceImageDir] = uigetfile('*VasculatureReference01.tif','select reference image file for registration');
    masterRefImage = double(imread([referenceImageDir,'\',referenceImageFN]));
else
    masterRefImage = nan;
end

bgname = cell(1,numsessions);
bgdir = cell(1,numsessions);
for n = 1:numsessions
    [fn{n},movdirectories{n}] = uigetfile('*.raw','select one of the movie files files');
    switch cameraID
        case 'dalsa'
            if contains(fn{n},'tif')
                rootnames{n} = strrep(fn{n},'.tif','');
                temp = strfind(fn{n},'-');
                rootnames{n} = fn{n}(1:(temp(1)-1));
            elseif contains(fn{n},'Raw')
                rootnames{n} = strrep(fn{n},'.Raw','');
                temp = strfind(fn{n},'-');
                rootnames{n} = fn{n}(1:(temp(1)-1));
            end
    end
    [metanames{n},metadirectories{n}] = uigetfile('*.mat','select metadatafile');
    [metanamesBG{n},metadirectoriesBG{n}] = uigetfile('*.mat','select background metadatafile');
    cd(movdirectories{n})
    imsizes(n,:) = input('what are the dimensions of the images? ');
    pixelbinning(n) = input('what binning to apply to the images? ');

    curmov = loadRawStack_v1(rootnames{n},imsizes(n,:),1:6,pixelbinning(n));
    %% select the windowmask:
    roiselectfig = figure;
    im = median(curmov,3);
    imagesc(im);
    roi = drawpolygon;
    windowmasks{n} = createMask(roi);
    %% preload background
    [bgname{n},bgdir{n}] = uigetfile('*ackground*','select background image');
    close(roiselectfig);
end
cd(savedirectory)
save([savename,'_preselect2.mat'],"bgdir","windowmasks","metanames","datatype","refsession",...
    "metadirectories","fn","movdirectories","rootnames","imsizes","cameraID",...
    "numsessions","pixelbinning","savename","savedirectory","bgname","homedir","movtype","stimtype","masterRefImage")





